//
//  ViewController.m
//  UI08_homework(通讯录)
//
//  Created by Hanlingyu on 16/7/25.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "MyCell.h"
#import "DetailViewController.h"
#import "AddStudentViewController.h"
#import "Person.h"

#define kTableViewCellReuse @"Reuse"
@interface ViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    AddStudent
>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *addButton;
@property (nonatomic, strong) NSMutableArray *arr;

@property (nonatomic, strong) NSMutableArray *personArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [UINavigationBar appearance].barTintColor = [UIColor colorWithRed:0.052 green:0.683 blue:1.000 alpha:1.000];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.052 green:0.683 blue:1.000 alpha:1.000];
    
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    
    // 创建添加联系人的按钮
    self.addButton = [self createAddButton];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_addButton];
    // 处理数据源
    self.personArray = [self handleDataSource];
    NSLog(@"%@", self.personArray);
    // 创建tableView
    [self createTableView];
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.arr removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationRight];
    }
}




// 创建添加联系人的按钮
- (UIButton *)createAddButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button setBackgroundImage:[UIImage imageNamed:@"addStudent"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addStudent:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

// 创建talbeView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 100;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:kTableViewCellReuse];
}
// 添加联系人的点击方法
- (void)addStudent:(UIButton *)button {
    AddStudentViewController *addStudentVC = [[AddStudentViewController alloc] init];
    [self.navigationController pushViewController:addStudentVC animated:YES];
    addStudentVC.delegate = self;
}

// 处理数据源
- (NSMutableArray *)handleDataSource {
    NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"DLS160710" ofType:@"plist"];
    NSArray *array = [NSMutableArray arrayWithContentsOfFile:dataPath];
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        
        Person *per = [[Person alloc] init];
        [per setValuesForKeysWithDictionary:dic];
        [arr addObject:per];
        
        
        
    }
    return arr;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _personArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
//    cell.nameLabel.text = self.arr[indexPath.row][@"name"];
//    cell.phoneNumberLabel.text = self.arr[indexPath.row][@"phoneNumber"];
//    cell.headImageView.image = [UIImage imageNamed:@"headerImage.jpg"];
    Person *person = self.personArray[indexPath.row];
    cell.nameLabel.text = person.name;
    cell.phoneNumberLabel.text = person.phoneNumber;
    cell.headImageView.image = [UIImage imageNamed:@"headerImage.jpg"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
//    detailVC.studentDic = self.arr[indexPath.row];
    detailVC.person = self.personArray[indexPath.row];
}

- (void)sendValue:(Person *)person {
    [self.personArray addObject:person];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
